<%--
  Created by IntelliJ IDEA.
  User: ThinkPad
  Date: 2022/11/22
  Time: 17:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎注册</title>
    <link href="css/register.css" rel="stylesheet">
</head>
<body>

<div class="form-div">
    <div class="reg-content">
        <h1>欢迎注册</h1>
        <span>已有帐号？</span> <a href="login.html">登录</a>
    </div>
    <form id="reg-form" action="/userdemo/regServlet" method="post">

        <table>

            <tr>
                <td>用户名</td>
                <td class="inputs">
                    <input name="username" type="text" id="username" onblur="fnSelectUser()">
                    <br>
                    <span id="username_err" class="err_msg">${errMessage}</span>
                </td>

            </tr>

            <tr>
                <td>密码</td>
                <td class="inputs">
                    <input name="password" type="password" id="password">
                    <br>
                    <span id="password_err" class="err_msg" style="display: none">密码格式有误</span>
                </td>
            </tr>


            <tr>
                <td>验证码</td>
                <td class="inputs">
                    <input name="checkCode" type="text" id="checkCode">
                    <img id="checkCodeImg" src="/userdemo/checkCodeServlet">
                    <a href="#" id="changeImg" >看不清？</a>
                </td>
            </tr>

        </table>

        <div class="buttons">
            <input value="注 册" type="submit" id="reg_btn">
        </div>
        <br class="clear">
    </form>

</div>
<script>
    document.getElementById("changeImg").onclick = function () {
        //路径后面添加时间戳的目的是避免浏览器进行缓存静态资源
        document.getElementById("checkCodeImg").src = "/userdemo/checkCodeServlet?"+new Date().getMilliseconds();
    }

    function fnSelectUser(){
        // 获取文本框的值
        var username = document.getElementById("username").value;
        console.log("fnSelectUser...username:"+username);
        // 发送 ajax请求，请求Servlet验证用户名
        //1. 创建核心对象
        var xhttp;
        if (window.XMLHttpRequest) {
            xhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        //2. 发送请求  GET请求，携带用户名
        // get的提交数据的方式
        xhttp.open("GET", "http://localhost:8080/userdemo/selectUserServlet?username="+username);
        xhttp.send();

        //3. 获取响应，因为是异步的，所以通过如下代码拿到Servlet返回的数据
        xhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                // 返回的数据 resultText返回值是什么？
                var resultText = this.responseText;
                console.log("resultText",resultText)
                if(resultText == "true"){
                    //用户名存在，显示提示信息  document.getElementById("username_err")  <span>用户名重名！！！</span>
                    // 让组件显示
                    document.getElementById("username_err").style.display = '';
                    // 给组件标签，中间加东西
                    document.getElementById("username_err").innerHTML = "用户名重名！！！";
                }else {
                    //用户名不存在 ，清楚提示信息
                    document.getElementById("username_err").style.display = 'none';
                    // 把原来里面信息抹掉
                    document.getElementById("username_err").innerHTML = "";
                }



            }
        };
    }

</script>
</body>
</html>